#include <cstdio>
#include <cstring>

#include "Data.h"

#include <sys/types.h>
#include <zconf.h>

extern unsigned int data[20];

int main(int argc, char* argv[]) {
    printf("Welcome to the CTFLearn Ramada Reversing Challenge!\n");

    pid_t p1 = getpid();
    pid_t p2 = getppid();
    printf("\tpid : %10d 0x%08x\n", p1, p1);
    printf("\tppid: %10d 0x%08x\n", p2, p2);

    if (argc == 1)
    {
        printf("Usage: Ramada CTFlearn{kernel}\n");
        return 1;
    }

    const char* userflag = argv[1];
    if (strncmp("CTFlearn{", userflag, 9) != 0)
    {
        printf("Error: Your flag must start with CTFlearn{\n");
        return 2;
    }

    size_t n = strlen(userflag);
    if (userflag[n-1] != '}')
    {
        printf("Error: Your flag must end with '}'\n");
        return 3;
    }

    if (n != 31)
    {
        printf("Your flag is the wrong length dude!\n");
        return 4;
    }

    InitData();

    char buffer[64];
    memset(buffer, 0, 64);
    strncpy(buffer, userflag+9, 21);

    printf("Your Flag Kernel: %s\n", buffer);

    int status = CheckFlag(buffer);

    if (status == 0)
    {
        printf("Woot Woot! You found the flag! %s\n", userflag);
    }

    printf("All Done Ramada!\n");

    return 0;
}
